package www.dianbaozu.com.ui.my

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentMyBinding
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.UseInfoEntity
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.imgUrl
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.extension.observeObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.NoTitleAdapter
import www.wen.com.common.lib.util.SpUtil

/**
 * @author WEN
 * @Description:主页我的
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class MyFragment : BaseLoadingFragment<FragmentMyBinding>(), View.OnClickListener {

    private val mBaseApiViewModel: BaseApiViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun getLayoutId(): Int = R.layout.fragment_my

    override fun isEnableLoadingHelper(): Boolean = true

    @SuppressLint("SetTextI18n")
    override fun lazyInitView() {
        dataBinding.apply {
            ivAvatar.post {
                setImageUrl(ivAvatar, SpUtil.decodeString(KeyHelper.MMKV_AVATAR)!!.imgUrl(), placeholder = R.drawable.user_default_icon)
            }
            tvNickname.text = SpUtil.decodeString(KeyHelper.MMKV_NICKNAME)
            dataBinding.tvId.text = "ID:${SpUtil.decodeString(KeyHelper.MMKV_ID)}"
        }
        observeListen()
    }

    @SuppressLint("SetTextI18n")
    private fun observeListen() {
        observeObject(KeyHelper.REFRESH_MY_KEY, this, UseInfoEntity::class.java) {
            dataBinding.let {
                setImageUrl(it.ivAvatar, avatar.imgUrl(), placeholder = R.drawable.user_default_icon)
                it.tvNickname.text = nickName
                it.tvId.text = "ID:$id"
            }
        }
    }

    override fun getTitleBarUniversalAdapter(): NoTitleAdapter = NoTitleAdapter(requireActivity(), true)

    override fun onClick(v: View) {
        when (v.id) {
            R.id.strAboutUs -> {
                nav(R.id.action_mainFragment_to_aboutUsFragment)
            }
            R.id.strHelpCenter -> {
                nav(R.id.action_mainFragment_to_helpCenterFragment)
            }
            R.id.strAccountSettings -> {
                nav(R.id.action_mainFragment_to_accountSettingsFragment)
            }
            R.id.userInfo -> {
                nav(R.id.action_mainFragment_to_personalDetailsFragment)
            }
        }
    }

}



